package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseConfirmDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.EmployeeLoanContent;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.entity.TravelListContent;
import com.isunland.managesystem.utils.MyUtils;
import java.util.HashMap;
import org.json.JSONException;

public class ShowManageReimburseListDetailFragment extends ManageReimburseListDetailFragment {
  public static final String EXTRA_TRAVELLIST_CONTENT =
      "com.isunland.managesystem.ui.EXTRA_TRAVELLIST_CONTENT";
  public static final String EXTRA_EMPLOYEELOAN_CONTENT =
      "com.isunland.managesystem.ui.EXTRA_EMPLOYEELOAN_CONTENT";

  private static final int REQUEST_DELETE = 0;
  private static final int REQUEST_ALTER = 1;//修改
  private TravelListContent mTravelListContent;
  private EmployeeLoanContent mEmployeeLoanContent;
  private String mDataStatus;

  // 创建菜单
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_employee_loan, menu);
    menu.getItem(2).setVisible(false).setEnabled(false);
    menu.getItem(3).setVisible(false).setEnabled(false);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BaseVolleyActivity) getActivity();
    mTravelListContent =
        (TravelListContent) getArguments().getSerializable(EXTRA_TRAVELLIST_CONTENT);
    mEmployeeLoanContent =
        (EmployeeLoanContent) getArguments().getSerializable(EXTRA_EMPLOYEELOAN_CONTENT);
    mDataStatus = mEmployeeLoanContent.getDataStatus();
    //			LogUtil.i(mTravelListContent.getARRIVE_TIME());
    //			LogUtil.i(mTravelListContent.toString());

  }

  // 初始化控件
  protected void initData() {
    // 详情
    tvappendDate.setText(mTravelListContent.getFEE_DATE());
    tvfareType.setText(mTravelListContent.getFEE_KIND());

    etfeeAmount.setText(mTravelListContent.getFEE_AMOUNT());
    etbillNum.setText(mTravelListContent.getBILL_NUM());
    etbillNo.setText(mTravelListContent.getBILL_NO());
    etfeeNote.setText(mTravelListContent.getFEE_NODE());
    etnote.setText(mTravelListContent.getNOTE());
    tvInvestigateContent.setText(mTravelListContent.getINVESTIGATE_CONTENT());

    ibappendDate.setVisibility(View.GONE);
    ibfareType.setVisibility(View.GONE);

    etfeeAmount.setEnabled(false);
    etbillNum.setEnabled(false);
    etbillNo.setEnabled(false);
    etfeeNote.setEnabled(false);
    etnote.setEnabled(false);
  }

  // 菜单点击事件
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      // 修改
      case R.id.menu_item_alter:
        if ("new".equals(mDataStatus) || "abort".equals(mDataStatus)) {
          Intent costIntent = new Intent(getActivity(), AddManageReimburseListActivity.class);
          costIntent.putExtra(EditTravelReimburseListDetailFragment.EXTRA_TRAVELLIST_CONTENT,
              mTravelListContent);
          costIntent.putExtra(AddManageReimburseListActivity.EXTRA_TYPE,
              AddManageReimburseListActivity.EXTRA_EDITCONTENTTYPE);
          startActivityForResult(costIntent, REQUEST_ALTER);
        } else {
          Toast.makeText(getActivity(), R.string.draftOrAbort, Toast.LENGTH_SHORT).show();
        }
        break;
      // 删除
      case R.id.menu_item_delete:
        if ("new".equals(mDataStatus) || "abort".equals(mDataStatus)) {
          DialogFragment dialog = null;
          FragmentManager fm = getActivity().getSupportFragmentManager();
          dialog = BaseConfirmDialogFragment.newInstance(R.string.deleteConfirm);
          dialog.setTargetFragment(ShowManageReimburseListDetailFragment.this, REQUEST_DELETE);
          dialog.show(fm, "");
        } else {
          Toast.makeText(getActivity(), R.string.draftOrAbort, Toast.LENGTH_SHORT).show();
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DELETE) {

      postDelete();//删除

      return;
    }
    if (requestCode == REQUEST_ALTER) {

      getActivity().setResult(Activity.RESULT_OK);
      getActivity().finish();

      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  //删除子表方法
  private void postDelete() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_DELETE_TRAVEL_LIST);

    HashMap<String, String> params = new HashMap<String, String>();

    params.put("id", mTravelListContent.getID());
    params.put("tableName", "imsoa.manage_fee_out");
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        SuccessMessage success = new Gson().fromJson(arg0, SuccessMessage.class);

        if (null == success || null == success.getResult()) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        String successType = success.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();

          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  // fragment之间传值
  public static ShowManageReimburseListDetailFragment newInstance(TravelListContent item,
      EmployeeLoanContent mEmployeeLoanContent) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_TRAVELLIST_CONTENT, item);
    args.putSerializable(EXTRA_EMPLOYEELOAN_CONTENT, mEmployeeLoanContent);
    ShowManageReimburseListDetailFragment fragment = new ShowManageReimburseListDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
